package com.cloud.imgCloud.config;

import com.cloud.imgCloud.common.R;
import com.cloud.imgCloud.exception.BusinessException;
import com.cloud.imgCloud.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 全局异常处理类
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalControllerAdvice {
    @ExceptionHandler(Exception.class)
    public R<String> handleException(Exception e) {
        log.error("系统异常", e);
        return R.fail(ErrorCode.SYSTEM_ERROR);
    }

    @ExceptionHandler(RuntimeException.class)
    public R<String> handleRuntimeException(RuntimeException e) {
        log.error("运行时异常", e);
        return R.fail(ErrorCode.OPERATION_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public R<String> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数异常", e);
        return R.fail(ErrorCode.PARAMS_ERROR);
    }

    @ExceptionHandler(BusinessException.class)
    public R<String> handleBusinessException(BusinessException e) {
        log.error("业务异常", e);
        return new R<>(e.getCode(), e.getMessage());
    }
}
